package com.it.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.it.domain.Brand;
import com.it.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表现层
 */
@Slf4j
@RestController
@RequestMapping("/brands")
//@RestControllerAdvice+ 自定义类 实现 ResponseBodyAdvice 拦截接口返回数据，进行统一封装
public class BrandController {

    @Autowired
    private BrandService brandService;
    //创建日志记录对象
//    private static final Logger log = LoggerFactory.getLogger(BrandController.class);
    @PostMapping
    public Result add(@RequestBody Brand brand){
        boolean flag = brandService.add(brand);
      /*

             log.info("info");
             log.debug("debug");
             log.warn("warn");
       */
        return new Result(flag ? Code.ADD_OK : Code.ADD_ERR, flag);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable  Integer id) {
        boolean flag = brandService.delete(id);
        return new Result(flag ? Code.DELETEBYID_OK : Code.DELETEBYID_ERR, flag);
    }

    @PutMapping
    public Result update(@RequestBody Brand brand){
        boolean flag = brandService.update(brand);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    @GetMapping
    public Result selectAll() {

        List<Brand> brandList = brandService.selectAll();
        Integer code = brandList != null ? Code.SELECT_OK : Code.SELECT_ERR;
        String msg =brandList!=null?"数据查询成功":"数据查询失败，请重试";
        return new Result(code,brandList,msg);

    }

    @GetMapping("{id}")
    public Result selectById(@PathVariable Integer id) {
        Brand brand = brandService.selectById(id);
        Integer code = brand != null ? Code.SELECT_OK : Code.SELECT_ERR;
        String msg =brand!=null?"数据查询成功":"数据查询失败，请重试";
        return new Result(code,brand,msg);

    }
    @GetMapping("{currentPage}/{pageSize}")
    public Result selectPage(@PathVariable int currentPage,@PathVariable int pageSize,Brand brand) {
//        System.out.println("参数=>>"+brand);
       IPage<Brand> brandList = brandService.selectPage(currentPage,pageSize,brand);
        Integer code = brandList != null ? Code.SELECT_PAGE_OK : Code.SELECT_PAGE_ERR;
        String msg =brandList!=null?"分页成功":"分页失败，请重试";
        return new Result(code,brandList,msg);
    }


}
